package netudp;

import java.io.IOException;
import java.net.*;

public class J_UdpClient {
    public static void main(String[] args){
        DatagramSocket dSocket;
        DatagramPacket inPacket;
        DatagramPacket outPacket;
        InetAddress sAddr;
        byte[] inBuffer = new byte[100];
        byte[] outBuffer;
        String s;

        try {
            dSocket = new DatagramSocket();
//            if (args.length == 0){
//                sAddr = InetAddress.getByName("127.0.0.1");
//            }else{
//                sAddr = InetAddress.getByName(args[0]);
//            }
            sAddr = InetAddress.getByName("192.168.5.100");
            s = "请求连接23231";
            outBuffer = s.getBytes();
            outPacket = new DatagramPacket(outBuffer,outBuffer.length,sAddr,8000);
            dSocket.send(outPacket);//发送数据

            inPacket = new DatagramPacket(inBuffer,inBuffer.length);
            dSocket.receive(inPacket);//接收数据
            s = new String(inPacket.getData(),0,inPacket.getLength());
            System.out.println("接收到服务器端信息："+s);

            dSocket.close();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
